package com.gzu.finalwork.admin.controller;

import com.gzu.finalwork.admin.service.SensitiveOperationLogService;
import com.gzu.finalwork.common.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 敏感操作日志控制器
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class SensitiveOperationLogController {

    @Autowired
    private SensitiveOperationLogService sensitiveOperationLogService;

    /**
     * 分页查询敏感操作日志
     * @param pageNum 页码，默认1
     * @param pageSize 每页大小，默认10
     * @param operationType 操作类型（可选）
     * @param operatorUsername 操作人用户名（可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @return 日志列表
     */
    @GetMapping("/sensitive-logs")
    public Object getSensitiveLogs(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize,
                                 @RequestParam(required = false) String operationType,
                                 @RequestParam(required = false) String operatorUsername,
                                 @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                 @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        try {
            return AjaxResult.success(sensitiveOperationLogService.getLogsByPage(
                    pageNum, pageSize, operationType, operatorUsername, startTime, endTime));
        } catch (Exception e) {
            return AjaxResult.error(500, "获取敏感操作日志失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询敏感操作日志详情
     * @param id 日志ID
     * @return 日志详情
     */
    @GetMapping("/sensitive-logs/{id}")
    public Object getSensitiveLogDetail(@PathVariable Long id) {
        try {
            return AjaxResult.success(sensitiveOperationLogService.getLogById(id));
        } catch (Exception e) {
            return AjaxResult.error(500, "获取敏感操作日志详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取敏感操作日志统计信息
     * @return 统计数据
     */
    @GetMapping("/sensitive-logs/statistics")
    public Object getSensitiveLogStatistics() {
        try {
            return AjaxResult.success(sensitiveOperationLogService.getLogStatistics());
        } catch (Exception e) {
            return AjaxResult.error(500, "获取敏感操作日志统计失败：" + e.getMessage());
        }
    }

    /**
     * 清理指定天数之前的敏感操作日志
     * @param days 保留天数
     * @return 清理结果
     */
    @DeleteMapping("/sensitive-logs/clean")
    public Object cleanSensitiveLogs(@RequestParam(defaultValue = "30") int days) {
        try {
            LocalDateTime beforeTime = LocalDateTime.now().minusDays(days);
            int cleanedCount = sensitiveOperationLogService.cleanLogsBefore(beforeTime);
            return AjaxResult.success("清理完成，共清理了 " + cleanedCount + " 条日志记录");
        } catch (Exception e) {
            return AjaxResult.error(500, "清理敏感操作日志失败：" + e.getMessage());
        }
    }
} 